package com.testmanager.testmananger.quantitative.service;

import com.testmanager.testmananger.quantitative.constants.IteraSearchStatus;
import com.testmanager.testmananger.quantitative.constants.IterationStatus;
import com.testmanager.testmananger.quantitative.domain.*;
import com.testmanager.testmananger.quantitative.dto.*;
import com.testmanager.testmananger.quantitative.mapper.IterationInfoMapper;
import com.testmanager.testmananger.quantitative.mapper.PageBaseMapper;
import com.testmanager.testmananger.quantitative.repository.*;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@Slf4j

public class IterationInfoService {

    @Autowired
    IterationInfoRepository iterationInfoRepository;
    @Autowired
    IterationInfoCustom iterationInfoCustom;
    @Autowired
    DemandInfoService demandInfoService;
    @Autowired
    DevelopInfoService developInfoService;
    @Autowired
    TestInfoService testInfoService;
    @Autowired
    BugInfoService bugInfoService;
    @Autowired
    WorkService workService;
    @Autowired
    IterationStatusChangeRecordRepository iterationStatusChangeRecordRepository;

    public PageableDto findIterationPage(IterationSearchDto search){
        log.info("查找迭代：{}",search);

        search.getStatus();

        Page<IterationInfoEntity> pages = iterationInfoCustom.search(search);
        List<IterationInfoDto> IterationInfodtos = pages.getContent().stream()
                .map(t -> {
                       long id =t.getId();
                   return IterationInfoMapper.toIterationInfoDto(t, demandInfoService.getDemandInfo(id),
                           developInfoService.getDevelopInfoWithTaskInfo(id),
                           testInfoService.getTestInfoWithTaskInfo(id),
                           bugInfoService.getBugsByItertionId(id));

                }).collect(Collectors.toList());
        return PageBaseMapper.pageExchange(pages, IterationInfodtos);
        //PageBaseMapper.
    }
    public IterationInfoDto getIterationDtoDetail(long id){
        IterationInfoEntity entity = iterationInfoRepository.findById(id);
        return IterationInfoMapper.toIterationInfoDto(entity, demandInfoService.getDemandInfo(id)
        , developInfoService.getDevelopInfoWithTaskInfo(id),testInfoService.getTestInfoWithTaskInfo(id),bugInfoService.getBugsByItertionId(id));

    }


    public PageableDto getIterationPage(){
        Page<IterationInfoEntity> pages = iterationInfoCustom.getPages();
        List<IterationInfoDto> IterationInfodtos = pages.getContent().stream()
                .map(t -> {
                    long id =t.getId();
                    return IterationInfoMapper.toIterationInfoDto(t, demandInfoService.getDemandInfo(id),
                            developInfoService.getDevelopInfoWithTaskInfo(id),
                            testInfoService.getTestInfoWithTaskInfo(id),
                            bugInfoService.getBugsByItertionId(id));

                }).collect(Collectors.toList());
        return PageBaseMapper.pageExchange(pages, IterationInfodtos);
        //PageBaseMapper.
    }

   @Transactional
    public PageableDto saveIteration(IterationInfoDto iterationInfoDto){
        log.info("保存迭代基础信息：{}",iterationInfoDto);
        long id = iterationInfoDto.getId();
        IterationInfoEntity srcIntera;


       if (id != 0){
              switch (IterationStatus.getValueOf(iterationInfoDto.getStatus())){
                  case DEVELOPING://开发中
                      developInfoService.saveDevelopInfoWithoutTaskInfo(id,  iterationInfoDto.getDevelop().setPracticeTimeFrom(LocalDateTime.now()));
                      break;
                  case WAITTESTER://开发完成
                      developInfoService.saveDevelopInfoWithoutTaskInfo(id,  iterationInfoDto.getDevelop().setPracticeTimeTo(LocalDateTime.now()));
                      break;
                  case TESTING://提测
                      testInfoService.saveTestInfoWithoutTaskInfo(id, iterationInfoDto.getTest().setPracticeTimeFrom(LocalDateTime.now()));
                      break;
                  case WAITONLINE://测试完成
                      testInfoService.saveTestInfoWithoutTaskInfo(id, iterationInfoDto.getTest().setPracticeTimeTo(LocalDateTime.now()));
                      break;
              }
           IterationInfoEntity byId = iterationInfoRepository.findById(iterationInfoDto.getId());
           iterationInfoRepository.save( IterationInfoMapper.toIterationInfoEntity(byId,iterationInfoDto));
            return getIterationPage();
        }
        IterationInfoEntity iterationInfoEntity = iterationInfoRepository.save( IterationInfoMapper.toIterationInfoEntity(new IterationInfoEntity(),iterationInfoDto));
        long newId = iterationInfoEntity.getId();
        developInfoService.saveDevelopInfoWithoutTaskInfo(newId, new DevelopInfoDto().setWorkers(new ArrayList<>()));
        demandInfoService.saveDemandInfo(newId, iterationInfoDto.getDemand().setName(iterationInfoDto.getName()).setWorker(new WorkerDto()));
        testInfoService.saveTestInfoWithoutTaskInfo(newId, new TestInfoDto().setWorkers(new ArrayList<>()));
        return getIterationPage();
    }

    public boolean saveIterationStatusChangeRecord(Long iterationId,int fromStatus,int toStatus){
        IterationStatusChangeRecordEntity iterationStatusChangeRecordEntity = new IterationStatusChangeRecordEntity();
        iterationStatusChangeRecordEntity.setFromStatus(IterationStatus.getValueOf(fromStatus))
                .setToStatus(IterationStatus.getValueOf(toStatus))
                .setIterationID(iterationId);
        iterationStatusChangeRecordRepository.save(iterationStatusChangeRecordEntity);
        return true;
    }


}
